<template>
  <el-dialog :visible="visible" :title="title" @close="close" width="56%" :close-on-click-modal="false">
    <el-form ref="form" :model="user" label-position="top">
      <el-col :span="24">
        <el-col :span="11">
            <el-form-item
              label="用户名"
              prop="username"
              :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]"
            >
              <el-input v-model="user.username" placeholder="请输入用户名" :disabled="operation !== 'add'" clearable />
            </el-form-item>
        </el-col>
        <el-col :span="2"></el-col>
        <el-col :span="11">
          <el-form-item
            label="密码"
            prop="password"
            :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]"
            v-if="operation === 'add'"
          >
            <el-input type="password" v-model="user.password" placeholder="请输入密码" clearable />
          </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="24">
        <el-col :span="11">
          <el-form-item
            label="昵称"
            prop="nickname"
            :rules="[{ required: true, message: '请输入昵称', trigger: 'blur' }]"
          >
            <el-input v-model="user.nickname" placeholder="请输入昵称" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="2"></el-col>
        <el-col :span="11">
          <el-form-item
            label="头像"
            prop="avatar"
          >
            <el-upload
            class="avatar-uploader"
            action="/upload/img" :headers="headers"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="user.avatar" :src="user.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="24">
        <el-col :span="11">
          <el-form-item
            label="手机号"
            prop="phone"
            :rules="[{ required: true, message: '请输入手机号', trigger: 'blur' }]"
          >
            <el-input v-model="user.phone" placeholder="请输入手机号" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="2"></el-col>
        <el-col :span="11">
          <el-form-item
              label="邮箱号"
              prop="email"
            >
              <el-input v-model="user.email" placeholder="请输入邮箱号" clearable />
            </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="24">
        <el-col :span="11">
          <el-form-item
              label="年龄"
              prop="age"
            >
              <el-input-number v-model="user.age" :min="0" :max="200" label="请输入年龄"></el-input-number>
            </el-form-item>
        </el-col>
        <el-col :span="2"></el-col>
        <el-col :span="11">
            <el-form-item
              label="性别"
              prop="sex"
              :rules="[{ required: true, message: '请选择性别', trigger: 'blur' }]"
            >
              <el-select v-model="user.sex" placeholder="请选择性别" clearable style="width: 100%;">
              <el-option label="不详" :value="0" />
                <el-option label="男" :value="1" />
                <el-option label="女" :value="2" />
              </el-select>
            </el-form-item>
        </el-col>
      </el-col>
    </el-form>
    <span slot="footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'UserEdit',
  props: {
    operation: {
      default: 'add',
      validator: (value) => {
        return ['add', 'update'].indexOf(value) !== -1;
      },
    },
    visible: {
      type: Boolean,
      required: true,
    },
    user: {
      type: Object,
      required: true,
    },
  },
  data() {
    const token = localStorage.getItem('token');
    return {
      headers: {token: token},
    };
  },
  computed: {
    title() {
      return this.operation === 'add' ? '添加用户' : '修改用户';
    },
  },
  methods: {
    close() {
      this.$emit('update:visible', false);
    },
    async confirm() {
      try {
        await this.$refs.form.validate();
        this.$emit('confirm');
      } catch (e) {
        console.error(e);
      }
    },
    handleAvatarSuccess(res, file) {
      this.user.avatar = res.data;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    }
  },
};
</script>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 110px;
    height: 110px;
    display: block;
  }
  .el-col-2 {
    height: 1px;
  }
</style>